package com.yuedou.jylt.download;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
import android.widget.Toast;

import java.io.File;

/**
 * <p>Project:DownloadDemo</p>
 * <p>Package:PACKAGE_NAME</p>
 * <p>Description:下载 后台服务</p>
 * <p>Company: 公众号 Coder栈</p>
 *
 * @author ryandu
 * @date 2017/7/27
 */
public class DownloadService extends Service {

    private DownloadTask downloadTask;

    private String downloadUrl;

    private DownloadListener downloadListener = new DownloadListener() {//创建Download实例
        @Override
        public void onProgress(int progress) {
            //构建显示下载进度的通知，并触发通知
        }

        @Override
        public void onSuccess() {
            //下载成功将前台服务关闭，并创建一个下载成功的通知
            Toast.makeText(DownloadService.this,"Download Success",Toast.LENGTH_SHORT).show();
            downloadTask = null;
        }

        @Override
        public void onFailed() {
            Toast.makeText(DownloadService.this,"Download Failed",Toast.LENGTH_SHORT).show();
            downloadTask = null;
        }

        @Override
        public void onPaused() {
            Toast.makeText(DownloadService.this,"Download Paused",Toast.LENGTH_SHORT).show();
            downloadTask = null;
        }

        @Override
        public void onCanceled() {
            Toast.makeText(DownloadService.this,"Download Canceled",Toast.LENGTH_SHORT).show();
            downloadTask = null;
        }
    };

    public DownloadService() {
    }

    private DownloadBinder mBinder = new DownloadBinder();

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    public class DownloadBinder extends Binder{
        public void startDownload(String url, Context context,String version){
            try{
                if (downloadTask == null){
                    System.out.println("图片地址： " + url);
                    downloadTask = new DownloadTask(downloadListener,context,version);
                    downloadTask.execute(url);
                    Toast.makeText(DownloadService.this,"Downloading ... ",Toast.LENGTH_SHORT).show();
                }else{
                    System.out.println("图片地址11111： " + url);
                    downloadTask = new DownloadTask(downloadListener,context,version);
                    downloadTask.execute(url);
                    Toast.makeText(DownloadService.this,"Downloading ... ",Toast.LENGTH_SHORT).show();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        public void successDownload(){
            downloadTask = null;
        }

        public void pauseDownload(){
            if (downloadTask != null){
                downloadTask.pauseDownload();
            }else {
                if (downloadUrl != null){
                    //取消下载是删除文件，关闭通知
                    String fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/"));
                    String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
                    File file = new File(directory + fileName);
                    if (file.exists()){
                        file.delete();
                    }
                    Toast.makeText(DownloadService.this,"Canceled",Toast.LENGTH_SHORT).show();
                }
            }
        }

        public void cancelDownload(){
            if (downloadTask != null){
                downloadTask.cancelDownload();
            }
        }
    }
}
